library _;

import 'dart:convert';
import 'dart:io';

import 'package:audioplayers/audioplayers.dart';
import 'package:extended_text/extended_text.dart';
import 'package:extended_text_field/extended_text_field.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/Const/IFinal.dart';
import 'package:flutter_app/Extension/String++.dart';
import 'package:flutter_app/Extension/special_text/emoji_text.dart';
import 'package:flutter_app/Extension/special_text/my_special_text_span_builder.dart';
import 'package:flutter_app/Model/Enums.dart';
import 'package:flutter_app/Model/MessageModel.dart';
import 'package:flutter_app/Page/Main/Other/ShowPicture/main.dart';
import 'package:flutter_app/Page/Main/Other/ShowVideo/main.dart';
import 'package:flutter_app/Page/Message/Chat/extensions/ChatRecordButton.dart';
import 'package:flutter_app/Page/Message/Chat/extensions/ChatRecordMsg.dart';
import 'package:flutter_app/Util/App/AppUtils.dart';
import 'package:flutter_app/Util/AssetUtils.dart';
import 'package:flutter_app/Util/CompressUtils.dart';
import 'package:flutter_app/Util/MessageUtils.dart';
import 'package:flutter_app/Util/YOSS/YOSS.dart';
import 'package:flutter_yfree/YFree.dart';
import 'package:flutter_yim/YIM.dart';
import 'package:record/record.dart';
import 'package:video_compress/video_compress.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';

part 'controller.dart';

part 'extensions/MessageGesture.dart';

part 'extensions/MessageUpload.dart';

part 'extensions/MessageView.dart';

part 'view.dart';

enum ChatInputType { Idle, Inputing, Face, Voice, Function }

class ChatPage extends YPage {
  ChatPage({required this.serviceName, required this.serviceAvatar, required this.serviceID});

  //初始化变量
  final String serviceName, serviceAvatar, serviceID;

  //需要dispose的变量
  final scrollController = ScrollController();
  final textEditingController = TextEditingController();
  final focusNode = FocusNode();

  //其它变量
  List<MessageModel> messages = [], tempMessages = [];
  ChatInputType chatInputType = ChatInputType.Idle;
  bool isPullRefreshing = false;
  final player = AudioPlayer();

  @override
  void yOnCreate() {
    controller_clearUnReadCount();
    controller_sendMessageReceipt();
    controller_loadMessages();
    controller_addListeners();
    controller_broadcasts();
  }

  @override
  void yOnDestory() {
    controller_clearUnReadCount();
    textEditingController.dispose();
    focusNode.dispose();
    scrollController.dispose();
    player.dispose();
  }

  @override
  Widget yBuild(BuildContext context) {
    return Container(
      color: IFinal.Colors.pageBG,
      child: Column(
        children: [
          YAppBar(title: serviceName, right: YButton(child: Icon(Icons.more_horiz_rounded), onTap: () => controller_more())),
          Expanded(
            child: GestureDetector(
              child: Stack(
                alignment: Alignment.topCenter,
                children: [
                  view_messages(),
                  Positioned(top: 0, child: view_pullLoading()),
                ],
              ),
              onPanDown: (details) {
                yCloseInputMethod();
                setState(() => chatInputType = ChatInputType.Idle);
              },
            ),
          ),
          view_input(),
          view_face(),
          view_function(),
        ],
      ),
    );
  }
}
